Cooperators have low degrees
#Define degrees of isolation
isolationDegree = 2
#number of iterations per arm
iterations = 500
modelForPrediction = "random forest" #"linear" or "random forest"
# List of manipulating parameters of experiments
#L : number of rounds
#V : Visible or not
#A : Income of a rich-group subject
#B : Income of a poor-group subject
#R : Probability to be assigned to a rich group
#I : Number of the same-parameter trial
R = 0.5
I = 0
L = 10
trends.df = data.frame()
for(A in c(1150,700,500)){
for(V in c(0,1)){
V = V
A = A
if(A==1150){B = 200} #high inequality
if(A==700){B = 300} #low inequality
if(A==500){B = 500} #no inequality
if(modelForPrediction=="random forest"){
source(paste(rootdir,"R/models.R",sep="/"))
if(V==0){
model1<-model1.invisible(redo=FALSE)
model2<-model2.invisible(redo=FALSE)
model3<-model3(redo=FALSE)
}
if(V==1){
model1<-model1.visible(redo=FALSE)
model2<-model2.visible(redo=FALSE)
model3<-model3(redo=FALSE)
}
}
df.netIntLowDegree = data.frame(
coopFrac = NULL,
avgCoop = NULL,
avgCoopFinal = NULL,
percentIsolation = NULL,
isolation = NULL,
percentIsolationC = NULL,
percentIsolationD = NULL,
nCommunities = NULL,
communitySize = NULL,
assortativityInitial = NULL,
assortativityFinal = NULL,
conversionRate = NULL,
conversionToD = NULL,
conversionToC = NULL,
transitivity = NULL,
degree = NULL,
degreeC = NULL,
degreeD = NULL,
meanConversionToD = NULL,
meanConversionToC = NULL,
degreeLost = NULL,
degreeLostC = NULL,
degreeLostD = NULL
)
#Here, factionCoop=0 will be the control: no rearranging of nodes will take place
for(frac in c(0,0.25,0.5,0.75,1)){
#nodes in the top fractionCoop degrees will automatically be a cooperator
fractionCoop = frac
coopFrac = NULL
avgCoop = NULL
homophilyC = NULL
homophilyD = NULL
heterophily = NULL
avgCoopFinal = NULL
percentIsolation = NULL
isolation = NULL
percentIsolationC = NULL
percentIsolationD = NULL
nCommunities = NULL
communitySize = NULL
assortativityInitial = NULL
assortativityFinal = NULL
conversionRate = NULL
conversionToD = NULL
conversionToC = NULL
transitivity = NULL
degree = NULL
degreeC = NULL
degreeD = NULL
meanConversionToD = NULL
meanConversionToC = NULL
degreeLost = NULL
degreeLostC = NULL
degreeLostD = NULL
avg_wealth = NULL
gini = NULL
for(m in c(1:iterations)){
# Section 1. NOTES, packages, and Parameters
#Importing library
library(igraph) # for network graphing
library(reldist) # for gini calculatio
library(boot) # for inv.logit calculation
#Two prefixed functions
#rank
rank1 = function(x) {rank(x,na.last=NA,ties.method="average")[1]} #a smaller value has a smaller rank.
#gini mean difference (a.k.a. mean difference: please refer to https://stat.ethz.ch/pipermail/r-help/2003-April/032782.html)
gmd = function(x) {
x1 = na.omit(x)
n = length(x1)
tmp = 0
for (i in 1:n) {
for (j in 1:n) {
tmp <- tmp + abs(x1[i]-x1[j])
}
}
answer = tmp/(n*n)
return(answer)
}
# List of fixed parameters of experiments (assumptions)
#Rewiring rate = 0.3
#GINI coefficient (can be known by A or B)
GINI = 0*as.numeric(A==500) + 0.2*as.numeric(A %in% c(700,850)) + 0.4*as.numeric(A ==1150)
#Collecting data frame (final output data frame)
result = data.frame(round=0:L,n_par=NA,n_A=NA,avg_coop=NA,avg_degree=NA,avg_wealth=NA,gini=NA,gmd=NA,avg_coop_A=NA,avg_degree_A=NA,avg_wealth_A=NA,gini_A=NA,gmd_A=NA,avg_coop_B=NA,avg_degree_B=NA,avg_wealth_B=NA,gini_B=NA,gmd_B=NA,isolation=NA,percentIsolation=NA,meanConversionToD=NA,meanConversionToC=NA,degreeLost=NA,degreeLostC=NA,degreeLostD=NA)
#_A is for a richer group and _B is for a poorer group
#####################################################
# Section 1.5: Practice rounds 1 to 2, to determine C/D in round 1
N = 17 # median of the number of participants over rounds.
node_rp0 = data.frame(ego_id=1:N, round=0)
node_import = node_rp0
for (k in 1:2){
node_rX = node_import #Importing data
node_rX$round = node_rX$round + 1
node_rX[is.na(node_rX$prev_degree)==1,"prev_degree"] = 0
node_rX[is.na(node_rX$prev_local_rate_coop)==1,"prev_local_rate_coop"] = 0
#Only this calculation needs to change from Round 1
if (k==1) {
node_rX$prob_coop = inv.logit(1.099471)
} else {
node_rX$prob_coop = inv.logit((-0.02339288) + (1.46068980)*as.numeric(node_rX$prev_coop==1))
}
node_rX$coop = apply(data.frame(node_rX$prob_coop),1,function(x) {sample(1:0,1,prob=c(x,(1-x)))})
node_rX$prev_coop = node_rX$coop
assign(paste("coop_rp",k, sep=""),node_rX$coop)
#For the loop
node_import = node_rX
}
#cooperation rate in the practice rounds
coop_rp = apply(cbind(coop_rp1,coop_rp2),1,mean)
#####################################################
# Section 2: Round 0 (Agents and environments)
#Node data generation
N = 17 # median of the number of participants over rounds.
node_r0 = data.frame(ego_id=1:N, round=0)
node_r0$coop_rp = ifelse(coop_rp==1,"C","D")
node_r0$group = sample(c("rich","poor"),N,replace=TRUE,prob=c(R,1-R)) #R is defined as the probability to be assigned to the rich group
node_r0$initial_wealth = ifelse(node_r0$group=="rich",A,B)
#Link data generation
ego_list = NULL
for (i in 1:N) { ego_list = c(ego_list,rep(i,N)) }
link_r0 = data.frame(ego_id=ego_list,alt_id=rep(1:N,N))
link_r0 = link_r0[(link_r0$ego_id < link_r0$alt_id),] #The link was bidirectional, and thus the half and self are omitted.
link_r0$connected = sample(0:1,dim(link_r0)[1],replace=TRUE,prob=c(0.7,0.3)) #Initial rewiring rate is fixed, 0.3
link_r0c_ego = link_r0[link_r0$connected==1,]
link_r0c_alt = link_r0[link_r0$connected==1,]
colnames(link_r0c_alt) = c("alt_id","ego_id","connected")
link_r0c = rbind(link_r0c_ego,link_r0c_alt) #this is bidirectional (double counted) for connected ties.
link_r0c = link_r0c[order(link_r0c$ego_id),]
link_r0c$alternumber = NA #putting the number for each alter in the same ego
link_r0c[1,]$alternumber = 1
for (i in 1:(dim(link_r0c)[1]-1))
{if (link_r0c[i,]$ego_id == link_r0c[i+1,]$ego_id)
{link_r0c[i+1,]$alternumber = link_r0c[i,]$alternumber + 1}
else
{link_r0c[i+1,]$alternumber = 1}
#print(i)
}
link_r0c2 = reshape(link_r0c, direction = "wide", idvar=c("ego_id","connected"), timevar="alternumber")
link_r0c2$initial_degree = apply(link_r0c2[,colnames(link_r0c2)[substr(colnames(link_r0c2),1,6) == "alt_id"]],1,function(x){length(na.omit(x))}) #Degree of each ego
link_r0c2[is.na(link_r0c2$initial_degree)==1,"initial_degree"] = 0
#Reflect the degree and initial local gini coefficient into the node data
node_r0 = merge(x=node_r0,y=link_r0c2,all.x=TRUE,all.y=FALSE,by="ego_id")
node_r0$initial_avg_env_wealth = NA
node_r0$initial_local_gini = NA #local gini coefficient of the ego and connecting alters
node_r0$initial_rel_rank = NA #local rank of ego among the ego and connecting alters (divided by the number of the go and connecting alters)
for (i in 1:(dim(node_r0)[1])){
node_r0[i,]$initial_avg_env_wealth = mean(na.omit(node_r0[node_r0$ego_id %in%
node_r0[i,colnames(node_r0)[substr(colnames(node_r0),1,6) %in% c("ego_id","alt_id")]],"initial_wealth"]))
node_r0[i,]$initial_local_gini = gini(na.omit(node_r0[node_r0$ego_id %in% node_r0[i,colnames(node_r0)[substr(colnames(node_r0),1,6)
%in% c("ego_id","alt_id")]],"initial_wealth"]))
node_r0[i,]$initial_rel_rank = rank1(na.omit(node_r0[node_r0$ego_id %in% node_r0[i,colnames(node_r0)[substr(colnames(node_r0),1,6)
%in% c("ego_id","alt_id")]],"initial_wealth"]))/length(na.omit(node_r0[node_r0$ego_id %in%
node_r0[i,colnames(node_r0)[substr(colnames(node_r0),1,6) %in% c("ego_id","alt_id")]],"initial_wealth"]))
}
#Finalization of round 0 and Visualization
#plot(graph.data.frame(link_r0[link_r0$connected==1,],directed=F)) #plot.igraph
node_r0$everIsolated = 0
node_r0$maxDegreeLost = NA
result[result$round==0,2:25] = c(length(node_r0$ego_id),length(node_r0[node_r0$group=="rich",]$ego_id),NA,mean(node_r0$initial_degree),mean(node_r0$initial_wealth),gini(node_r0$initial_wealth),gmd(node_r0$initial_wealth),NA,mean(node_r0[node_r0$group=="rich",]$initial_degree),mean(node_r0[node_r0$group=="rich",]$initial_wealth),gini(node_r0[node_r0$group=="rich",]$initial_wealth),gmd(node_r0[node_r0$group=="rich",]$initial_wealth),NA,mean(node_r0[node_r0$group=="poor",]$initial_degree),mean(node_r0[node_r0$group=="poor",]$initial_wealth),gini(node_r0[node_r0$group=="poor",]$initial_wealth),gmd(node_r0[node_r0$group=="poor",]$initial_wealth),
as.numeric(ifelse(is.na(table(node_r0$initial_degree<=isolationDegree)["TRUE"]),0,1)),
as.numeric(sum(node_r0$everIsolated)/length(node_r0$ego_id)),
NA,
NA,
NA,NA,NA
)
#For the loop at the next round (for round 1, the initial one is the same as the previous [1 prior] one)
node_import = node_r0
node_import$initial_coop = NA
node_import$prev_coop = NA
node_import$prev_wealth = node_import$initial_wealth
node_import$prev_degree = node_import$initial_degree
node_import$prev_avg_env_wealth = node_import$initial_avg_env_wealth
node_import$prev_local_gini = node_import$initial_local_gini
node_import$prev_rel_rank = node_import$initial_rel_rank
node_import$prev_local_rate_coop = NA
link_import = link_r0
#####################################################
# Section 3: Rounds 1 to 10 or more (behaviors in simulation: the equation of cooperation is different at round 1 because of no history)
#3-1: Cooperation phase
for (k in 1:L)
{
node_rX = node_import #Importing data
node_rX$round = node_rX$round + 1
node_rX[is.na(node_rX$prev_degree)==1,"prev_degree"] = 0
node_rX[is.na(node_rX$prev_local_rate_coop)==1,"prev_local_rate_coop"] = 0
#Only this calculation needs to change from Round 1
if(modelForPrediction=="linear"){
if (k==1) {
node_rX$prob_coop = as.numeric(V==0)*inv.logit((-1.816665) + (2.086067)*coop_rp1 + (1.800153)*coop_rp2) + as.numeric(V==1)*inv.logit((-2.031577) + (2.427157)*coop_rp1 + (1.684193)*coop_rp2 + (-1.528851)*GINI)
} else {
node_rX$prob_coop = as.numeric(V==0 & node_rX$prev_coop==0)*inv.logit(-1.039916) + as.numeric(V==0 & node_rX$prev_coop==1)*inv.logit(2.062023) + as.numeric(V==1 & node_rX$prev_coop==0)*inv.logit((-0.2574838)*as.numeric(node_rX$prev_avg_env_wealth - node_rX$prev_wealth > 0) + (-1.214198)*GINI + (2.508148)*GINI*as.numeric(node_rX$prev_avg_env_wealth - node_rX$prev_wealth > 0) + (-0.9749075)) + as.numeric(V==1 & node_rX$prev_coop==1)*inv.logit((- 0.6197254)*as.numeric(node_rX$prev_avg_env_wealth - node_rX$prev_wealth > 0) + (-0.7480261)*GINI + (1.169674)*GINI*as.numeric(node_rX$prev_avg_env_wealth - node_rX$prev_wealth > 0) + (1.356784))
}
}
if(modelForPrediction=="random forest"){
if (k==1) {
if(V==1){node_rX$prob_coop = predict(model1,
newdata=
data.frame(
behavior.p1 = coop_rp1,
behavior.p2 = coop_rp2,
gini = GINI
),
type = "prob"
)[[1]]$C}
else if(V==0){node_rX$prob_coop = predict(model1,
newdata=
data.frame(
behavior.p1 = coop_rp1,
behavior.p2 = coop_rp2
),
type = "prob"
)[[1]]$C}
} else {
if(V==1){node_rX$prob_coop = predict(model2,
newdata=
data.frame(
prevCoop = node_rX$prev_coop,
gini = GINI,
alterPrevWealth = node_rX$prev_avg_env_wealth,
egoPrevWealth = node_rX$prev_wealth
),
type = "prob"
)[[1]]$C}
else if(V==0){node_rX$prob_coop = predict(model2,
newdata=
data.frame(
prevCoop = node_rX$prev_coop,
alterPrevWealth = node_rX$prev_avg_env_wealth,
egoPrevWealth = node_rX$prev_wealth
),
type = "prob"
)[[1]]$C}
}
}
#####rearrange node degrees before round 1 depending on cooperation in practice rounds!
if(k==1){
if(fractionCoop==0){
node_rX$prob_coop
node_rX$coop = apply(data.frame(node_rX$prob_coop),1,function(x) {sample(1:0,1,prob=c(x,(1-x)))})
coop_rp_init = coop_rp
}
if(fractionCoop>0){
prob_coop_df = NULL
nodesCoop = NULL
#nodesCoop = node_rX$prev_degree<=quantile(node_rX$prev_degree,fractionCoop) #assign low-degree nodes to cooperators
#assign defectors to designated nodes
nodesCoop = node_rX$prev_degree<=floor(quantile(node_rX$prev_degree,fractionCoop)) & node_rX$prev_degree>=floor(quantile(node_rX$prev_degree,fractionCoop-0.25))
prob_coop_df =
data.frame(
prob_coop = rev(node_rX$prob_coop[order(coop_rp)]),
node_number = c(which(!nodesCoop),which(nodesCoop))
)
node_rX$prob_coop = prob_coop_df[order(prob_coop_df$node_number),]$prob_coop
#coop_rp of the rearranged nodes
coop_rp_init = rev(coop_rp[order(coop_rp)])[order(prob_coop_df$node_number)]
node_rX$coop = apply(data.frame(node_rX$prob_coop),1,function(x) {sample(1:0,1,prob=c(x,(1-x)))})
}
} else {
node_rX$coop = apply(data.frame(node_rX$prob_coop),1,function(x) {sample(1:0,1,prob=c(x,(1-x)))})
}
if (k==1) {
node_rX$initial_coop = node_rX$coop
} else {
node_rX$initial_coop = node_rX$initial_coop
}
node_rX$cost = (-50)*node_rX$coop*node_rX$prev_degree
node_rX$n_coop_received = NA
for (i in 1:(dim(node_rX)[1]))
{
node_rX[i,]$n_coop_received = sum(node_rX[node_rX$ego_id %in% node_rX[i,colnames(node_rX)[substr(colnames(node_rX),1,6) ==
"alt_id"]],"coop"])
}
node_rX$benefit = 100*node_rX$n_coop_received
node_rX$payoff = node_rX$cost + node_rX$benefit
node_rX$wealth = node_rX$prev_wealth + node_rX$payoff
node_rX$rel_rank = NA
node_rX$local_rate_coop = NA
for (i in 1:dim(node_rX)[1])
{
node_rX[i,]$rel_rank = rank1(na.omit(node_rX[node_rX$ego_id %in% node_rX[i,colnames(node_rX)[substr(colnames(node_rX),1,6) %in%
c("ego_id","alt_id")]],"wealth"]))/length(na.omit(node_rX[node_rX$ego_id %in%
node_rX[i,colnames(node_rX)[substr(colnames(node_rX),1,6) %in% c("ego_id","alt_id")]],"wealth"]))
node_rX[i,]$local_rate_coop = mean(na.omit(node_rX[node_rX$ego_id %in% node_rX[i,colnames(node_rX)[substr(colnames(node_rX),1,6) %in%
c("ego_id","alt_id")]],"coop"]))
}
node_rX$growth = as.numeric((node_rX$wealth/node_rX$prev_wealth) > 1)
node_rX = node_rX[,c("ego_id","round","group","prev_degree","initial_wealth","initial_local_gini","initial_coop","coop","wealth","rel_rank","local_rate_coop","growth","everIsolated","maxDegreeLost")] #Pruning the previous-round data (degree is not updating yet)
#3-2: Rewiring phase
# 30% of ties (unidirectional) are being rewired
link_rX_1 = link_import #Importing data (bidirectioanl ego-alter [ego_id < alter_id])
colnames(link_rX_1) = c("ego_id","alt_id","prev_connected")
link_rX_1$challenge = sample(0:1,dim(link_rX_1)[1],replace=TRUE,prob=c(0.7,0.3)) # The bidirectional ties being rewired are selected (rewiring rate = 0.3).
ego_node_data =
node_rX[,c("ego_id","wealth","coop","prev_degree","initial_wealth","initial_local_gini","initial_coop","rel_rank","local_rate_coop","growth")]
colnames(ego_node_data) =
c("ego_id","ego_wealth","ego_coop","ego_prev_degree","ego_initial_wealth","ego_initial_local_gini","ego_initial_coop","ego_rel_rank","ego_local_rate_coop","ego_growth")
alt_node_data =
node_rX[,c("ego_id","wealth","coop","prev_degree","initial_wealth","initial_local_gini","initial_coop","rel_rank","local_rate_coop","growth")]
colnames(alt_node_data) =
c("alt_id","alt_wealth","alt_coop","alt_prev_degree","alt_initial_wealth","alt_initial_local_gini","alt_initial_coop","alt_rel_rank","alt_local_rate_coop","alt_growth")
link_rX_2 = merge(x=link_rX_1,y=ego_node_data,all.x=TRUE,all.y=FALSE,by="ego_id")
link_rX_3 = merge(x=link_rX_2,y=alt_node_data,all.x=TRUE,all.y=FALSE,by="alt_id")
link_rX_3$choice = sample(c("ego","alt"),dim(link_rX_3)[1],replace=TRUE,prob=c(0.5,0.5)) #decision maker for breaking a link, which is a unilateral decision
#ego_prob: probability of choosing to connect when challenged (asked)
if(modelForPrediction=="linear"){
link_rX_3$ego_prob = inv.logit((0.5134401)*link_rX_3$prev_connected + (-0.852406)*link_rX_3$ego_coop + (2.96549)*link_rX_3$alt_coop + (-0.1808545))
link_rX_3$alt_prob = inv.logit((0.5134401)*link_rX_3$prev_connected + (-0.852406)*link_rX_3$alt_coop + (2.96549)*link_rX_3$ego_coop + (-0.1808545))}
if(modelForPrediction=="random forest"){
link_rX_3$ego_prob = predict(model3,
newdata=
data.frame(
previouslyconnected = link_rX_3$prev_connected,
ego_behavior = link_rX_3$ego_coop,
alter_behavior = link_rX_3$alt_coop
),
type = "prob"
)[[1]]$C
link_rX_3$alt_prob = predict(model3,
newdata=
data.frame(
previouslyconnected = link_rX_3$prev_connected,
ego_behavior = link_rX_3$alt_coop,
alter_behavior = link_rX_3$ego_coop
),
type = "prob"
)[[1]]$C
}
link_rX_3$prob_connect = ifelse(link_rX_3$prev_connected == 1, ifelse(link_rX_3$choice == "ego", link_rX_3$ego_prob,
link_rX_3$alt_prob), link_rX_3$ego_prob*link_rX_3$alt_prob)
link_rX_3$connect_update = apply(data.frame(link_rX_3$prob_connect),1, function(x) {sample(1:0,1,prob=c(x,(1-x)))})
link_rX_3$connected = ifelse(link_rX_3$challenge==0,link_rX_3$prev_connected,link_rX_3$connect_update)
link_rX = link_rX_3[,c("ego_id","alt_id","connected")] #pruning and data is updated
#Reflect the degree and local gini coefficient into the node data
link_rXc_ego = link_rX[link_rX$connected==1,]
link_rXc_alt = link_rX[link_rX$connected==1,]
colnames(link_rXc_alt) = c("alt_id","ego_id","connected")
link_rXc = rbind(link_rXc_ego,link_rXc_alt)
link_rXc = link_rXc[order(link_rXc$ego_id),]
link_rXc$alternumber = NA
link_rXc[1,]$alternumber = 1
for (i in 1:(dim(link_rXc)[1]-1))
{
if (link_rXc[i,]$ego_id == link_rXc[i+1,]$ego_id)
{
link_rXc[i+1,]$alternumber = link_rXc[i,]$alternumber + 1
}
else
{
link_rXc[i+1,]$alternumber = 1
}
#print(i)
}
link_rXc2 = reshape(link_rXc, direction = "wide", idvar=c("ego_id","connected"), timevar="alternumber")
link_rXc2$degree = apply(link_rXc2[,colnames(link_rXc2)[substr(colnames(link_rXc2),1,3) == "alt"]],1,function(x) {length(na.omit(x))})
node_rX_final = merge(x=node_rX[,c("ego_id","round","group","initial_wealth","initial_local_gini","initial_coop","coop","wealth","growth","everIsolated","maxDegreeLost")],y=link_rXc2,all.x=TRUE,all.y=FALSE,by="ego_id")
node_rX_final[is.na(node_rX_final$degree)==1,"degree"] = 0
node_rX_final$avg_env_wealth = NA
node_rX_final$local_gini = NA #needs to be updated because the social network changes at the rewiring phase
node_rX_final$local_rate_coop = NA
node_rX_final$rel_rank = NA
for (i in 1:dim(node_rX_final)[1])
{
node_rX_final[i,]$avg_env_wealth = mean(na.omit(node_rX_final[node_rX_final$ego_id %in%
node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in% c("ego_id","alt_id")]],"wealth"]))
node_rX_final[i,]$local_gini = gini(na.omit(node_rX_final[node_rX_final$ego_id %in%
node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in% c("ego_id","alt_id")]],"wealth"]))
node_rX_final[i,]$local_rate_coop = mean(na.omit(node_rX_final[node_rX_final$ego_id %in%
node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in% c("ego_id","alt_id")]],"coop"]))
node_rX_final[i,]$rel_rank = rank1(na.omit(node_rX_final[node_rX_final$ego_id %in%
node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in%
c("ego_id","alt_id")]],"wealth"]))/length(na.omit(node_rX_final[node_rX_final$ego_id %in%
node_rX_final[i,colnames(node_rX_final)[substr(colnames(node_rX_final),1,6) %in% c("ego_id","alt_id")]],"wealth"]))
node_rX_final[i,]$everIsolated = ifelse(node_rX_final[i,]$everIsolated==1,1,ifelse(node_rX_final[i,]$degree<=isolationDegree,1,0))
node_rX_final[i,]$maxDegreeLost = pmax(node_r0[i,]$initial_degree - node_rX_final[i,]$degree, node_rX_final[i,]$maxDegreeLost, na.rm=TRUE)
}
#Finalization of round X and Visualization
#plot(graph.data.frame(link_rX[link_rX$connected==1,],directed=F)) #plot.igraph
result[result$round==k,2:25] =
c(length(node_rX_final$ego_id),length(node_rX_final[node_rX_final$group=="rich",]$ego_id),mean(node_rX_final$coop),mean(node_rX_final$degree),mean(node_rX_final$wealth),gini(node_rX_final$wealth),gmd(node_rX_final$wealth),mean(node_rX_final[node_rX_final$group=="rich",]$coop),mean(node_rX_final[node_rX_final$group=="rich",]$degree),mean(node_rX_final[node_rX_final$group=="rich",]$wealth),gini(node_rX_final[node_rX_final$group=="rich",]$wealth),gmd(node_rX_final[node_rX_final$group=="rich",]$wealth),mean(node_rX_final[node_rX_final$group=="poor",]$coop),mean(node_rX_final[node_rX_final$group=="poor",]$degree),mean(node_rX_final[node_rX_final$group=="poor",]$wealth),gini(node_rX_final[node_rX_final$group=="poor",]$wealth),gmd(node_rX_final[node_rX_final$group=="poor",]$wealth),
as.numeric(ifelse(is.na(table(node_rX_final$degree<=isolationDegree)["TRUE"]),0,1)),
as.numeric(sum(node_rX_final$everIsolated)/length(node_rX_final$ego_id)),
prop.table(table(node_rX_final[node_rX_final$initial_coop==1]$coop))["0"],
prop.table(table(node_rX_final[node_rX_final$initial_coop==0]$coop))["1"],
suppressWarnings({mean(node_rX_final$maxDegreeLost,na.rm=TRUE)}),
suppressWarnings({mean(node_rX_final[node_rX_final$initial_coop==1]$maxDegreeLost,na.rm=TRUE)}),
suppressWarnings({mean(node_rX_final[node_rX_final$initial_coop==0]$maxDegreeLost,na.rm=TRUE)})
)
#For the loop
node_import = node_rX_final
colnames(node_import)[colnames(node_import) %in%
c("coop","wealth","growth","degree","avg_env_wealth","local_gini","local_rate_coop","rel_rank")] =
c("prev_coop","prev_wealth","prev_growth","prev_degree","prev_avg_env_wealth","prev_local_gini","prev_local_rate_coop","prev_rel_rank")
link_import = link_rX
#print(paste0("Round ",k," is done."))
}
trends.df = rbind(trends.df,cbind(result[c("round","gini","gmd","avg_wealth","avg_coop","avg_degree")],V,GINI,fractionCoop))
link_rX_final = data.table::melt(setDT(node_rX_final),
measure = patterns('alt_id'),
variable.name = 'linkNumber',
value.name = c('alt_id'))
link_rX_final = data.frame(link_rX_final)[c("ego_id","alt_id")]
link_rX_final = link_rX_final[complete.cases(link_rX_final),]
link_rX_final = data.frame(t(unique(apply(link_rX_final, 1, function(x) sort(x))))) %>% distinct(X1, X2)
node_g_final = data.frame(node_rX_final)[c("ego_id","initial_coop","coop")]
node_g_final$initial_coop = factor(node_g_final$initial_coop)
g_rX_final = graph_from_data_frame(link_rX_final, directed = FALSE, vertices=node_g_final)
g_r0 = graph_from_data_frame(link_r0[link_r0$connected==1,][1:2], directed = FALSE, vertices=node_r0)
E(g_r0)$coopEdgeC = sapply(E(g_r0), function(e) prod(ifelse(V(g_r0)[inc(e)]$coop_rp=="C",1,0)))
E(g_r0)$coopEdgeD = sapply(E(g_r0), function(e) prod(ifelse(V(g_r0)[inc(e)]$coop_rp=="D",1,0)))
E(g_r0)$coopEdgeCD = sapply(E(g_r0), function(e) ifelse(sum(ifelse(V(g_r0)[inc(e)]$coop_rp=="C",1,0))==1,1,0))
#C-assortativity, defined as number of observed C-C edges out of total possible C-C edges
homophilyC[m] = sum(E(g_r0)$coopEdgeC) / (table(V(g_r0)$coop_rp)["C"]*(table(V(g_r0)$coop_rp)["C"]-1)/2)
#D-assortativity, defined as number of observed C-C edges out of total possible C-C edges
homophilyD[m] = sum(E(g_r0)$coopEdgeD) / (table(V(g_r0)$coop_rp)["D"]*(table(V(g_r0)$coop_rp)["D"]-1)/2)
#heterophily, defined as number of observed C-D edges out of total possible C-D edges
heterophily[m] = sum(E(g_r0)$coopEdgeCD) / (table(V(g_r0)$coop_rp)["C"]*table(V(g_r0)$coop_rp)["D"])
coopFrac[m] = fractionCoop
avgCoop[m] = prop.table(table(V(g_r0)$coop_rp))["C"]
avgCoopFinal[m] = result[result$round==10,]$avg_coop
percentIsolation[m] = max(result[result$round>=1,]$percentIsolation)
isolation[m] = max(result[result$round>=1,]$isolation)
#percentage of isolation among those who cooperated in both practice rounds
percentIsolationC[m] = sum(node_rX_final[coop_rp_init==1,]$everIsolated)/length(node_rX_final[coop_rp_init==1,]$everIsolated)
#percentage of isolation among those who defected at least once in practice rounds
percentIsolationD[m] = sum(node_rX_final[coop_rp_init<=0.5,]$everIsolated)/length(node_rX_final[coop_rp_init<=0.5,]$everIsolated)
nCommunities[m] = max(membership(cluster_louvain(g_rX_final)),na.rm=TRUE)
communitySize[m] = mean(table(membership(cluster_louvain(g_rX_final))),na.rm=TRUE)
assortativityInitial[m] = assortativity(g_r0, V(g_r0)$coop_rp == "C")
assortativityFinal[m] = assortativity(g_rX_final, V(g_r0)$coop_rp == "C")
conversionRate[m] = prop.table(table(V(g_rX_final)$coop == ifelse(V(g_r0)$coop_rp=="C","1","0")))["FALSE"]
conversionToD[m] = prop.table(table(V(g_rX_final)$coop[V(g_r0)$coop_rp == "C"]))["0"]
conversionToC[m] = prop.table(table(V(g_rX_final)$coop[V(g_r0)$coop_rp == "C"]))["1"]
transitivity[m] = mean(transitivity(g_rX_final, type="global"),na.rm=TRUE)
degree[m] = mean(igraph::degree(g_rX_final),na.rm=TRUE)
degreeC[m] = mean(igraph::degree(g_r0)[coop_rp_init==1],na.rm=TRUE)
degreeD[m] = mean(igraph::degree(g_r0)[coop_rp_init<=0.5],na.rm=TRUE)
meanConversionToD[m] = mean(result[result$round>=2,]$meanConversionToD, na.rm=TRUE)
meanConversionToC[m] = mean(result[result$round>=2,]$meanConversionToC, na.rm=TRUE)
degreeLost[m] = result[result$round==10,]$degreeLost
degreeLostC[m] = result[result$round==10,]$degreeLostC
degreeLostD[m] = result[result$round==10,]$degreeLostD
avg_wealth[m] = result[result$round==10,]$avg_wealth
gini[m] = result[result$round==10,]$gini
}
df.netIntLowDegree = rbind(df.netIntLowDegree,
data.frame(
coopFrac = coopFrac,
avgCoop = avgCoop,
avgCoopFinal = avgCoopFinal,
percentIsolation = percentIsolation,
isolation = isolation,
percentIsolationC = percentIsolationC,
percentIsolationD = percentIsolationD,
nCommunities = nCommunities,
communitySize = communitySize,
assortativityInitial = assortativityInitial,
assortativityFinal = assortativityFinal,
conversionRate = conversionRate,
conversionToD = conversionToD,
conversionToC = conversionToC,
homophilyC = homophilyC,
homophilyD = homophilyD,
heterophily = heterophily,
transitivity = transitivity,
degree = degree,
degreeC = degreeC,
degreeD = degreeD,
meanConversionToD = meanConversionToD,
meanConversionToC = meanConversionToC,
degreeLost = degreeLost,
degreeLostC = degreeLostC,
degreeLostD = degreeLostD,
avg_wealth = avg_wealth,
gini = gini
))
#plot(g_r0,vertex.color=V(g_rX_final)$initial_coop,vertex.label=ifelse(is.na(V(g_rX_final)$initial_coop),"NA",ifelse(V(g_rX_final)$initial_coop==1,"C","D")),main=paste("fracCoop=",frac,", round 0",sep=""))
#plot(g_rX_final,vertex.color=V(g_rX_final)$initial_coop,vertex.label=ifelse(is.na(V(g_rX_final)$initial_coop),"NA",ifelse(V(g_rX_final)$initial_coop==1,"C","D")),main=paste("fracCoop=",frac,", final round",sep=""))
}
sum.netIntLowDegree <- data.frame(
df.netIntLowDegree %>%
group_by(coopFrac) %>%
summarise(
mean.isolation = mean(isolation),
ci.isolation = 1.96 * sd(isolation)/sqrt(n()),
mean.percentIsolation = mean(percentIsolation),
ci.percentIsolation = 1.96 * sd(percentIsolation)/sqrt(n()),
mean.percentIsolationC = mean(percentIsolationC,na.rm=TRUE),
ci.percentIsolationC = 1.96 * sd(percentIsolationC,na.rm=TRUE)/sqrt(sum(isolation)),
mean.percentIsolationD = mean(percentIsolationD,na.rm=TRUE),
ci.percentIsolationD = 1.96 * sd(percentIsolationD,na.rm=TRUE)/sqrt(sum(isolation)),
mean.avgCoop = mean(avgCoop,na.rm=TRUE),
ci.avgCoop = 1.96 * sd(avgCoop,na.rm=TRUE)/sqrt(n()),
mean.avgCoopFinal = mean(avgCoopFinal,na.rm=TRUE),
ci.avgCoopFinal = 1.96 * sd(avgCoopFinal,na.rm=TRUE)/sqrt(n()),
mean.nCommunities = mean(nCommunities,na.rm=TRUE),
ci.nCommunities = 1.96 * sd(nCommunities,na.rm=TRUE)/sqrt(n()),
mean.communitySize = mean(communitySize,na.rm=TRUE),
ci.communitySize = 1.96 * sd(communitySize,na.rm=TRUE)/sqrt(n()),
mean.assortativityInitial = mean(assortativityInitial,na.rm=TRUE),
ci.assortativityInitial = 1.96 * sd(assortativityInitial,na.rm=TRUE)/sqrt(n()),
mean.assortativityFinal = mean(assortativityFinal,na.rm=TRUE),
ci.assortativityFinal = 1.96 * sd(assortativityFinal,na.rm=TRUE)/sqrt(n()),
mean.conversionRate = mean(conversionRate,na.rm=TRUE),
ci.conversionRate = 1.96 * sd(conversionRate,na.rm=TRUE)/sqrt(n()),
mean.conversionToD = mean(conversionToD,na.rm=TRUE),
ci.conversionToD = 1.96 * sd(conversionToD,na.rm=TRUE)/sqrt(n()),
mean.conversionToC = mean(conversionToC,na.rm=TRUE),
ci.conversionToC = 1.96 * sd(conversionToC,na.rm=TRUE)/sqrt(n()),
mean.homophilyC = mean(homophilyC,na.rm=TRUE),
ci.homophilyC = 1.96 * sd(homophilyC,na.rm=TRUE)/sqrt(n()),
mean.homophilyD = mean(homophilyD,na.rm=TRUE),
ci.homophilyD = 1.96 * sd(homophilyD,na.rm=TRUE)/sqrt(n()),
mean.heterophily = mean(heterophily,na.rm=TRUE),
ci.heterophily = 1.96 * sd(heterophily,na.rm=TRUE)/sqrt(n()),
mean.transitivity = mean(transitivity,na.rm=TRUE),
ci.transitivity = 1.96 * sd(transitivity,na.rm=TRUE)/sqrt(n()),
mean.degree = mean(degree,na.rm=TRUE),
ci.degree = 1.96 * sd(degree,na.rm=TRUE)/sqrt(n()),
mean.degreeC = mean(degreeC,na.rm=TRUE),
ci.degreeC = 1.96 * sd(degreeC,na.rm=TRUE)/sqrt(n()),
mean.degreeD = mean(degreeD,na.rm=TRUE),
ci.degreeD = 1.96 * sd(degreeD,na.rm=TRUE)/sqrt(n()),
mean.meanConversionToD = mean(meanConversionToD,na.rm=TRUE),
ci.meanConversionToD = 1.96 * sd(meanConversionToD,na.rm=TRUE)/sqrt(n()),
mean.meanConversionToC = mean(meanConversionToC,na.rm=TRUE),
ci.meanConversionToC = 1.96 * sd(meanConversionToC,na.rm=TRUE)/sqrt(n()),
mean.degreeLost = mean(degreeLost,na.rm=TRUE),
ci.degreeLost = 1.96 * sd(degreeLost,na.rm=TRUE)/sqrt(n()),
mean.degreeLostC = mean(degreeLostC,na.rm=TRUE),
ci.degreeLostC = 1.96 * sd(degreeLostC,na.rm=TRUE)/sqrt(n()),
mean.degreeLostD = mean(degreeLostD,na.rm=TRUE),
ci.degreeLostD = 1.96 * sd(degreeLostD,na.rm=TRUE)/sqrt(n()),
mean.avg_wealth = mean(avg_wealth,na.rm=TRUE),
ci.avg_wealth = 1.96 * sd(avg_wealth,na.rm=TRUE)/sqrt(n()),
mean.gini = mean(gini,na.rm=TRUE),
ci.gini = 1.96 * sd(gini,na.rm=TRUE)/sqrt(n())
)
)
kable(sum.netIntLowDegree[c(1:9)]) %>% kableExtra::kable_styling(font_size = 10)
kable(sum.netIntLowDegree[c(1,10:17)]) %>% kableExtra::kable_styling(font_size = 10)
kable(sum.netIntLowDegree[c(1,18:25)]) %>% kableExtra::kable_styling(font_size = 10)
kable(sum.netIntLowDegree[c(1,26:33)]) %>% kableExtra::kable_styling(font_size = 10)
kable(sum.netIntLowDegree[c(1,34:ncol(sum.netIntLowDegree))]) %>% kableExtra::kable_styling(font_size = 10)
compare_means(percentIsolation ~ coopFrac, data=df.netIntLowDegree)
compare_means(avgCoop ~ coopFrac, data=df.netIntLowDegree)
compare_means(avgCoopFinal ~ coopFrac, data=df.netIntLowDegree)
compare_means(nCommunities ~ coopFrac, data=df.netIntLowDegree)
compare_means(communitySize ~ coopFrac, data=df.netIntLowDegree)
compare_means(assortativityInitial ~ coopFrac, data=df.netIntLowDegree)
compare_means(assortativityFinal ~ coopFrac, data=df.netIntLowDegree)
#compare_means(conversionRate ~ coopFrac, data=df.netIntLowDegree)
#compare_means(conversionToD ~ coopFrac, data=df.netIntLowDegree)
#compare_means(conversionToC ~ coopFrac, data=df.netIntLowDegree)
#compare_means(degreeC ~ coopFrac, data=df.netIntLowDegree)
#compare_means(degreeD ~ coopFrac, data=df.netIntLowDegree)
#compare_means(meanConversionToD ~ coopFrac, data=df.netIntLowDegree)
#compare_means(meanConversionToC ~ coopFrac, data=df.netIntLowDegree)
#compare_means(degreeLost ~ coopFrac, data=df.netIntLowDegree)
#compare_means(degreeLostC ~ coopFrac, data=df.netIntLowDegree)
#compare_means(degreeLostD ~ coopFrac, data=df.netIntLowDegree)
summary(lm(percentIsolation ~ assortativityInitial, data=df.netIntLowDegree))
#plot(df.netIntLowDegree$assortativityInitial, df.netIntLowDegree$percentIsolation)
#percentIsolation
g.percentIsolation = ggbarplot(data=df.netIntLowDegree, x="coopFrac", y="percentIsolation", add = "mean_se", color="coopFrac") +
stat_compare_means(ref.group = "0", label = "p.signif", label.y = 0.098, method="t.test", color="black") +
labs(
title = paste("Isolation when defectors are assigned to 25% of nodes by degree, ","V=",V,", Gini=",GINI,sep=""),
x = "Degree percentile of nodes assigned to defectors ",
y = "Propoption of ever-isolated individuals") +
annotate("text", x=1, y=0.0990, label= "ref", color="black") +
annotate("text", x=2.4, y= -0.0022, label= "Lowest degree nodes assigned to defectors", size=2.5) +
geom_segment(aes(x = 3.3, y = -0.0024, xend = 3.7, yend = -0.0024), linewidth=0.2, arrow = arrow(length = unit(0.1, "cm"))) +
annotate("text", x=4.6, y= -0.0022, label= "Highest degree nodes assigned to defectors", size=2.5) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size=12),legend.position="none") +
coord_cartesian(ylim=c(0,0.10)) +
scale_x_discrete(labels=c('Control','0-25','25-50','50-75','75-100')) +
scale_color_manual(values = c('0' = "black",'0.25'="black",'0.5'="black",'0.75'="black",'1'="black")) +
geom_vline(xintercept = 1.5, linetype = "longdash")
print(g.percentIsolation)
#percentIsolationC
#percentage of isolation among those who cooperated in both practice rounds
g.percentIsolationC = ggbarplot(data=df.netIntLowDegree, x="coopFrac", y="percentIsolationC", add = "mean_se") +
stat_compare_means(ref.group = "0", label = "p.signif", label.y = 0.098, method="t.test", color="black") +
labs(
title = paste("Isolation among initial cooperators, ","V=",V,", Gini=",GINI,sep=""),
x = "Degree percentile of nodes assigned to defectors ",
y = "Propoption of ever-isolated individuals") +
annotate("text", x=1, y=0.0990, label= "ref", color="black") +
annotate("text", x=2.4, y= -0.0022, label= "Lowest degree nodes assigned to defectors", size=2.5) +
geom_segment(aes(x = 3.3, y = -0.0024, xend = 3.7, yend = -0.0024), linewidth=0.2, arrow = arrow(length = unit(0.1, "cm"))) +
annotate("text", x=4.6, y= -0.0022, label= "Highest degree nodes assigned to defectors", size=2.5) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size=12),legend.position="none") +
coord_cartesian(ylim=c(0,0.10)) +
scale_x_discrete(labels=c('Control','0-25','25-50','50-75','75-100')) +
scale_color_manual(values = c('0' = "black",'0.25'="black",'0.5'="black",'0.75'="black",'1'="black")) +
geom_vline(xintercept = 1.5, linetype = "longdash")
print(g.percentIsolationC)
#percentIsolationD
#percentage of isolation among those who defected at least once in practice rounds
g.percentIsolationD = ggbarplot(data=df.netIntLowDegree, x="coopFrac", y="percentIsolationD", add = "mean_se") +
stat_compare_means(ref.group = "0", label = "p.signif", label.y = 0.298, method="t.test", color="black") +
labs(
title = paste("Isolation among initial defectors, ","V=",V,", Gini=",GINI,sep=""),
x = "Degree percentile of nodes assigned to defectors ",
y = "Propoption of ever-isolated individuals") +
annotate("text", x=1, y=0.2990, label= "ref", color="black") +
annotate("text", x=2.4, y= -0.0062, label= "Lowest degree nodes assigned to defectors", size=2.5) +
geom_segment(aes(x = 3.3, y = -0.0064, xend = 3.7, yend = -0.0064), linewidth=0.2, arrow = arrow(length = unit(0.1, "cm"))) +
annotate("text", x=4.6, y= -0.0062, label= "Highest degree nodes assigned to defectors", size=2.5) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size=12),legend.position="none") +
coord_cartesian(ylim=c(0,0.30)) +
scale_x_discrete(labels=c('Control','0-25','25-50','50-75','75-100')) +
scale_color_manual(values = c('0' = "black",'0.25'="black",'0.5'="black",'0.75'="black",'1'="black")) +
geom_vline(xintercept = 1.5, linetype = "longdash")
print(g.percentIsolationD)
#avgCoopFinal
g.avgCoopFinal = ggbarplot(data=df.netIntLowDegree, x="coopFrac", y="avgCoopFinal", add = "mean_se") +
stat_compare_means(ref.group = "0", label = "p.signif", label.y = 0.98, method="t.test", color="black") +
labs(
title = paste("Cooperation in final round, ","V=",V,", Gini=",GINI,sep=""),
x = "Degree percentile of nodes assigned to defectors ",
y = "Propoption of cooperators in final round") +
annotate("text", x=1, y=0.990, label= "ref", color="black") +
annotate("text", x=2.4, y= -0.0212, label= "Lowest degree nodes assigned to defectors", size=2.5) +
geom_segment(aes(x = 3.3, y = -0.0214, xend = 3.7, yend = -0.0214), linewidth=0.2, arrow = arrow(length = unit(0.1, "cm"))) +
annotate("text", x=4.6, y= -0.0212, label= "Highest degree nodes assigned to defectors", size=2.5) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size=12),legend.position="none") +
coord_cartesian(ylim=c(0,1.0)) +
scale_x_discrete(labels=c('Control','0-25','25-50','50-75','75-100')) +
scale_color_manual(values = c('0' = "black",'0.25'="black",'0.5'="black",'0.75'="black",'1'="black")) +
geom_vline(xintercept = 1.5, linetype = "longdash")
print(g.avgCoopFinal)
#avg_wealth
g.avg_wealth = ggbarplot(data=df.netIntLowDegree, x="coopFrac", y="avg_wealth", add = "mean_se") +
stat_compare_means(ref.group = "0", label = "p.signif", label.y = 6800, method="t.test", color="black") +
labs(
title = paste("Wealth in final round, ","V=",V,", Gini=",GINI,sep=""),
x = "Degree percentile of nodes assigned to defectors ",
y = "Average wealth in final round") +
annotate("text", x=1, y=6900, label= "ref", color="black") +
annotate("text", x=2.4, y= -162, label= "Lowest degree nodes assigned to defectors", size=2.5) +
geom_segment(aes(x = 3.3, y = -164, xend = 3.7, yend = -164), linewidth=0.2, arrow = arrow(length = unit(0.1, "cm"))) +
annotate("text", x=4.6, y= -162, label= "Highest degree nodes assigned to defectors", size=2.5) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size=12),legend.position="none") +
coord_cartesian(ylim=c(0,7000)) +
scale_x_discrete(labels=c('Control','0-25','25-50','50-75','75-100')) +
scale_color_manual(values = c('0' = "black",'0.25'="black",'0.5'="black",'0.75'="black",'1'="black")) +
geom_vline(xintercept = 1.5, linetype = "longdash")
print(g.avg_wealth)
#gini
g.gini = ggbarplot(data=df.netIntLowDegree, x="coopFrac", y="gini", add = "mean_se") +
stat_compare_means(ref.group = "0", label = "p.signif", label.y = 0.48, method="t.test", color="black") +
labs(
title = paste("Gini coefficient in final round, ","V=",V,", Gini=",GINI,sep=""),
x = "Degree percentile of nodes assigned to defectors ",
y = "Gini coefficient in final round") +
annotate("text", x=1, y=0.490, label= "ref", color="black") +
annotate("text", x=2.4, y= -0.0112, label= "Lowest degree nodes assigned to defectors", size=2.5) +
geom_segment(aes(x = 3.3, y = -0.0114, xend = 3.7, yend = -0.0114), linewidth=0.2, arrow = arrow(length = unit(0.1, "cm"))) +
annotate("text", x=4.6, y= -0.0112, label= "Highest degree nodes assigned to defectors", size=2.5) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size=12),legend.position="none") +
coord_cartesian(ylim=c(0,0.50)) +
scale_x_discrete(labels=c('Control','0-25','25-50','50-75','75-100')) +
scale_color_manual(values = c('0' = "black",'0.25'="black",'0.5'="black",'0.75'="black",'1'="black")) +
geom_vline(xintercept = 1.5, linetype = "longdash")
print(g.gini)
#degree
g.degree = ggbarplot(data=df.netIntLowDegree, x="coopFrac", y="degree", add = "mean_se") +
stat_compare_means(ref.group = "0", label = "p.signif", label.y = 14.8, method="t.test", color="black") +
labs(
title = paste("Degree in final round, ","V=",V,", Gini=",GINI,sep=""),
x = "Degree percentile of nodes assigned to defectors ",
y = "Mean degree in final round") +
annotate("text", x=1, y=14.90, label= "ref", color="black") +
annotate("text", x=2.4, y= -0.312, label= "Lowest degree nodes assigned to defectors", size=2.5) +
geom_segment(aes(x = 3.3, y = -0.314, xend = 3.7, yend = -0.314), linewidth=0.2, arrow = arrow(length = unit(0.1, "cm"))) +
annotate("text", x=4.6, y= -0.312, label= "Highest degree nodes assigned to defectors", size=2.5) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size=12),legend.position="none") +
coord_cartesian(ylim=c(0,15)) +
scale_x_discrete(labels=c('Control','0-25','25-50','50-75','75-100')) +
scale_color_manual(values = c('0' = "black",'0.25'="black",'0.5'="black",'0.75'="black",'1'="black")) +
geom_vline(xintercept = 1.5, linetype = "longdash")
print(g.degree)
#transitivity
g.transitivity = ggbarplot(data=df.netIntLowDegree, x="coopFrac", y="transitivity", add = "mean_se") +
stat_compare_means(ref.group = "0", label = "p.signif", label.y = 0.98, method="t.test", color="black") +
labs(
title = paste("Transitivity in final round, ","V=",V,", Gini=",GINI,sep=""),
x = "Degree percentile of nodes assigned to defectors ",
y = "Transitivity in final round") +
annotate("text", x=1, y=0.99, label= "ref", color="black") +
annotate("text", x=2.4, y= -0.0212, label= "Lowest degree nodes assigned to defectors", size=2.5) +
geom_segment(aes(x = 3.3, y = -0.0214, xend = 3.7, yend = -0.0214), linewidth=0.2, arrow = arrow(length = unit(0.1, "cm"))) +
annotate("text", x=4.6, y= -0.0212, label= "Highest degree nodes assigned to defectors", size=2.5) +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5, size=12),legend.position="none") +
coord_cartesian(ylim=c(0,1.00)) +
scale_x_discrete(labels=c('Control','0-25','25-50','50-75','75-100')) +
scale_color_manual(values = c('0' = "black",'0.25'="black",'0.5'="black",'0.75'="black",'1'="black")) +
geom_vline(xintercept = 1.5, linetype = "longdash")
print(g.transitivity)
#initial C-assortativity
plotList <- lapply(
unique(df.netIntLowDegree$coopFrac),
function(key) {
if(key==0){
ggplot(data = df.netIntLowDegree[df.netIntLowDegree$coopFrac==key,], aes(x = homophilyC, y = percentIsolation)) +
geom_point() +
scale_x_continuous(paste("C-assortativity, ","Control",sep="")) +
scale_y_continuous("Proportion isolated") +
geom_smooth(method='lm', formula= y~x) +
stat_cor(method = "pearson")
}
else{
ggplot(data = df.netIntLowDegree[df.netIntLowDegree$coopFrac==key,], aes(x = homophilyC, y = percentIsolation)) +
geom_point() +
scale_x_continuous(paste("C-assortativity, degree %ile = ",key,sep="")) +
scale_y_continuous("Proportion isolated") +
geom_smooth(method='lm', formula= y~x) +
stat_cor(method = "pearson")
}
}
)
plot= ggarrange(plotlist=plotList)
print(annotate_figure(plot, top = text_grob(paste("Proportion of ever-isolated individuals, ","V=",V,", Gini=", GINI, sep=""), color = "black", face = "bold", size = 10)))
lapply(unique(df.netIntLowDegree$coopFrac),
function(key) {
if(key==0){
reg = lm(percentIsolation ~ homophilyC + degreeD, data=df.netIntLowDegree[df.netIntLowDegree$coopFrac==key,])
print(paste("Regression on proportion of ever-isolated individuals, ","Control"," ; ",sep=""))
print(summary(reg)[4]$coefficients)
}
else{
reg = lm(percentIsolation ~ homophilyC + degreeD, data=df.netIntLowDegree[df.netIntLowDegree$coopFrac==key,])
print(paste("Regression on proportion of ever-isolated individuals, degree %ile = ",key," ; ",sep=""))
print(summary(reg)[4]$coefficients)
}
}
)
#initial D-assortativity
plotList <- lapply(
unique(df.netIntLowDegree$coopFrac),
function(key) {
if(key==0){
ggplot(data = df.netIntLowDegree[df.netIntLowDegree$coopFrac==key,], aes(x = homophilyD, y = percentIsolation)) +
geom_point() +
scale_x_continuous(paste("D-assortativity, ","Control",sep="")) +
scale_y_continuous("Proportion isolated") +
geom_smooth(method='lm', formula= y~x) +
stat_cor(method = "pearson")
}
else{
ggplot(data = df.netIntLowDegree[df.netIntLowDegree$coopFrac==key,], aes(x = homophilyD, y = percentIsolation)) +
geom_point() +
scale_x_continuous(paste("D-assortativity, degree %ile = ",key,sep="")) +
scale_y_continuous("Proportion isolated") +
geom_smooth(method='lm', formula= y~x) +
stat_cor(method = "pearson")
}
}
)
plot= ggarrange(plotlist=plotList)
print(annotate_figure(plot, top = text_grob(paste("Proportion of ever-isolated individuals, ","V=",V,", Gini=", GINI, sep=""), color = "black", face = "bold", size = 10)))
lapply(unique(df.netIntLowDegree$coopFrac),
function(key) {
if(key==0){
reg = lm(percentIsolation ~ homophilyD + degreeD, data=df.netIntLowDegree[df.netIntLowDegree$coopFrac==key,])
print(paste("Regression on proportion of ever-isolated individuals, ","Control"," ; ",sep=""))
print(summary(reg)[4]$coefficients)
}
else{
reg = lm(percentIsolation ~ homophilyD + degreeD, data=df.netIntLowDegree[df.netIntLowDegree$coopFrac==key,])
print(paste("Regression on proportion of ever-isolated individuals, degree %ile = ",key," ; ",sep=""))
print(summary(reg)[4]$coefficients)
}
}
)
}
}
## Loading data last updated on 2023-01-20 20:37:15
## Call model1.invisible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 20:39:47
## Call model2.invisible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 22:02:59
## Call model3(redo=TRUE) to update data.


## Warning: Removed 1 rows containing non-finite values (`stat_summary()`).
## Warning: Removed 1 rows containing non-finite values (`stat_compare_means()`).






## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.15721723 0.016211154 9.698090 1.765017e-20
## homophilyC -0.03242498 0.031778198 -1.020353 3.080585e-01
## degreeD -0.01847735 0.002611508 -7.075358 5.108727e-12
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.17855156 0.013237408 13.488407 1.486169e-35
## homophilyC -0.06283228 0.040074099 -1.567903 1.175403e-01
## degreeD -0.02054586 0.003418054 -6.010980 3.572096e-09
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.17667645 0.014903616 11.8546034 1.011031e-28
## homophilyC -0.02615723 0.040105790 -0.6522059 5.145699e-01
## degreeD -0.02440924 0.003756461 -6.4979354 1.981662e-10
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.12583919 0.01402438 8.972885 5.925176e-18
## homophilyC -0.07453528 0.03390793 -2.198166 2.839759e-02
## degreeD -0.01098533 0.00290776 -3.777936 1.772975e-04
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.094032091 0.014427131 6.517726 1.755195e-10
## homophilyC -0.033410626 0.029638781 -1.127260 2.601763e-01
## degreeD -0.007550081 0.002543625 -2.968237 3.139659e-03
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).


## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.14530203 0.013176443 11.0274091 1.957591e-25
## homophilyD -0.01397525 0.020980906 -0.6660936 5.056616e-01
## degreeD -0.01712792 0.003333034 -5.1388372 3.985234e-07
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.16861517 0.011910417 14.1569496 1.861332e-38
## homophilyD -0.00634483 0.018484875 -0.3432444 7.315601e-01
## degreeD -0.02254410 0.003165001 -7.1229366 3.737788e-12
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.175248242 0.014431483 12.1434670 6.862618e-30
## homophilyD -0.008299338 0.018386244 -0.4513884 6.519069e-01
## degreeD -0.025293315 0.003384397 -7.4735075 3.557374e-13
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.180824e-01 0.01379805 8.557905411 1.452746e-16
## homophilyD 5.720351e-05 0.01534631 0.003727509 9.970274e-01
## degreeD -1.373924e-02 0.00281542 -4.879996309 1.433005e-06
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.090089277 0.01440599 6.253597 8.651274e-10
## homophilyD 0.014534457 0.01444435 1.006238 3.147908e-01
## degreeD -0.009220185 0.00232278 -3.969461 8.266033e-05
## Loading data last updated on 2023-01-20 21:50:22
## Call model1.visible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 21:54:00
## Call model2.visible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 22:02:59
## Call model3(redo=TRUE) to update data.


## Warning: Removed 1 rows containing non-finite values (`stat_summary()`).
## Warning: Removed 1 rows containing non-finite values (`stat_compare_means()`).






## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.15183304 0.014454328 10.504331 1.931068e-23
## homophilyC -0.03757887 0.029297676 -1.282657 2.002114e-01
## degreeD -0.02133913 0.002291528 -9.312185 4.062481e-19
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.19126996 0.012658743 15.109712 1.018795e-42
## homophilyC -0.14438222 0.040428255 -3.571320 3.897341e-04
## degreeD -0.02456433 0.003456466 -7.106777 4.147197e-12
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.15294191 0.012171621 12.565451 1.220070e-31
## homophilyC -0.04224108 0.034686873 -1.217783 2.238843e-01
## degreeD -0.02431251 0.003084606 -7.881885 2.054623e-14
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.11288116 0.011963552 9.435422 1.496270e-19
## homophilyC -0.03631893 0.026478495 -1.371639 1.707948e-01
## degreeD -0.01488520 0.002509453 -5.931649 5.623142e-09
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.084281201 0.011404611 7.390099 6.257535e-13
## homophilyC -0.044368999 0.023910770 -1.855607 6.410135e-02
## degreeD -0.008177277 0.001949374 -4.194822 3.234392e-05
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).


## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.139324970 0.011647322 11.9619749 3.826354e-29
## homophilyD -0.003886736 0.020208488 -0.1923318 8.475612e-01
## degreeD -0.020846701 0.003026866 -6.8872225 1.733098e-11
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.168883523 0.011545083 14.6281770 1.478244e-40
## homophilyD -0.008059677 0.019630162 -0.4105762 6.815603e-01
## degreeD -0.029792392 0.003199614 -9.3112460 4.067516e-19
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.148636854 0.011816399 12.5788619 1.091898e-31
## homophilyD -0.002029895 0.015174956 -0.1337662 8.936417e-01
## degreeD -0.026074513 0.002746096 -9.4951222 9.284222e-20
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.11128416 0.011796597 9.433582 1.518704e-19
## homophilyD -0.02330542 0.013490067 -1.727598 8.468149e-02
## degreeD -0.01539052 0.002311735 -6.657562 7.383984e-11
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.082111928 0.011407129 7.198299 2.276719e-12
## homophilyD -0.012800650 0.012359698 -1.035677 3.008587e-01
## degreeD -0.009340293 0.001813267 -5.151085 3.745945e-07
## Loading data last updated on 2023-01-20 20:37:15
## Call model1.invisible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 20:39:47
## Call model2.invisible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 22:02:59
## Call model3(redo=TRUE) to update data.


## Warning: Removed 1 rows containing non-finite values (`stat_summary()`).
## Warning: Removed 1 rows containing non-finite values (`stat_compare_means()`).






## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.15851791 0.016024853 9.8920044 3.536086e-21
## homophilyC -0.02777185 0.032480994 -0.8550186 3.929536e-01
## degreeD -0.01844878 0.002540512 -7.2618360 1.487397e-12
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.16248715 0.013523738 12.014959 2.261005e-29
## homophilyC -0.04873001 0.043190791 -1.128250 2.597587e-01
## degreeD -0.01775605 0.003692653 -4.808481 2.018728e-06
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.18634782 0.01488415 12.519887 1.888369e-31
## homophilyC -0.07580337 0.04241707 -1.787096 7.453129e-02
## degreeD -0.02295274 0.00377203 -6.084982 2.329061e-09
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.111158946 0.016113436 6.8985255 1.605032e-11
## homophilyC -0.006090188 0.035663282 -0.1707691 8.644748e-01
## degreeD -0.011732384 0.003379926 -3.4711959 5.632980e-04
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.09736599 0.014751759 6.6002970 1.054517e-10
## homophilyC 0.02239048 0.030928359 0.7239466 4.694391e-01
## degreeD -0.01067949 0.002521497 -4.2353756 2.719305e-05
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).


## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.14734797 0.012886215 11.4345428 4.976651e-27
## homophilyD -0.01427898 0.022358008 -0.6386519 5.233448e-01
## degreeD -0.01695236 0.003348825 -5.0621811 5.855369e-07
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.154712178 0.012196281 12.68519307 3.856533e-32
## homophilyD -0.001617506 0.020737396 -0.07799946 9.378599e-01
## degreeD -0.019556628 0.003380087 -5.78583527 1.278139e-08
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.177268876 0.014465528 12.2545734 2.402773e-30
## homophilyD 0.005782943 0.018577042 0.3112951 7.557071e-01
## degreeD -0.026461281 0.003361745 -7.8712931 2.222994e-14
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.110969242 0.015905165 6.976931 9.678646e-12
## homophilyD -0.005433689 0.018188443 -0.298744 7.652602e-01
## degreeD -0.011746606 0.003116876 -3.768711 1.837880e-04
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0993376674 0.014717489 6.74963405 4.158512e-11
## homophilyD 0.0003008627 0.015946495 0.01886701 9.849548e-01
## degreeD -0.0099097523 0.002339479 -4.23587973 2.715304e-05
## Loading data last updated on 2023-01-20 21:50:22
## Call model1.visible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 21:54:00
## Call model2.visible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 22:02:59
## Call model3(redo=TRUE) to update data.


## Warning: Removed 1 rows containing non-finite values (`stat_summary()`).
## Warning: Removed 1 rows containing non-finite values (`stat_compare_means()`).






## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.16845954 0.014239102 11.83077 1.280286e-28
## homophilyC -0.06814847 0.028861432 -2.36123 1.860016e-02
## degreeD -0.02273282 0.002257406 -10.07032 7.916384e-22
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.18143385 0.012797468 14.177324 1.474953e-38
## homophilyC -0.10007452 0.040871302 -2.448528 1.468868e-02
## degreeD -0.02474906 0.003494345 -7.082603 4.860509e-12
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.15123122 0.011490278 13.161668 3.740203e-34
## homophilyC -0.07922173 0.032745171 -2.419341 1.590664e-02
## degreeD -0.02176971 0.002911936 -7.476028 3.486674e-13
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.13036830 0.012376816 10.533266 1.487822e-23
## homophilyC -0.05109292 0.027393158 -1.865171 6.274652e-02
## degreeD -0.01729768 0.002596139 -6.662847 7.144242e-11
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.09117642 0.011155854 8.172967 2.517424e-15
## homophilyC -0.02899087 0.023389228 -1.239497 2.157466e-01
## degreeD -0.01008863 0.001906854 -5.290719 1.830240e-07
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).


## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.15034009 0.011507355 13.064695 1.006971e-33
## homophilyD 0.02019359 0.019965641 1.011417 3.123111e-01
## degreeD -0.02451522 0.002990492 -8.197722 2.117160e-15
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.166453994 0.011593874 14.3570648 2.370549e-39
## homophilyD -0.008283684 0.019713120 -0.4202117 6.745124e-01
## degreeD -0.028284497 0.003213136 -8.8027710 2.219465e-17
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.143558649 0.011202112 12.8153197 1.117229e-32
## homophilyD -0.005845829 0.014386071 -0.4063534 6.846582e-01
## degreeD -0.025030857 0.002603337 -9.6149120 3.494996e-20
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.12711690 0.01224940 10.3773953 5.726627e-23
## homophilyD -0.01315464 0.01400788 -0.9390891 3.481412e-01
## degreeD -0.01891586 0.00240047 -7.8800669 2.081373e-14
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.088590693 0.011149054 7.9460275 1.309246e-14
## homophilyD -0.001260098 0.012080072 -0.1043121 9.169639e-01
## degreeD -0.010998065 0.001772244 -6.2057286 1.151853e-09
## Loading data last updated on 2023-01-20 20:37:15
## Call model1.invisible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 20:39:47
## Call model2.invisible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 22:02:59
## Call model3(redo=TRUE) to update data.


## Warning: Removed 1 rows containing non-finite values (`stat_summary()`).
## Warning: Removed 1 rows containing non-finite values (`stat_compare_means()`).






## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.15694230 0.016392636 9.573951 4.885753e-20
## homophilyC -0.04027299 0.033226459 -1.212076 2.260604e-01
## degreeD -0.01720200 0.002598819 -6.619162 9.396739e-11
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.15760004 0.013559563 11.6227963 8.641346e-28
## homophilyC -0.01870310 0.043305206 -0.4318904 6.660084e-01
## degreeD -0.01823095 0.003702435 -4.9240428 1.155688e-06
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.17858485 0.014303614 12.485296 2.629469e-31
## homophilyC -0.06137530 0.040762660 -1.505674 1.327860e-01
## degreeD -0.02202858 0.003624908 -6.077002 2.439486e-09
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.12249030 0.016399736 7.4690412 3.657194e-13
## homophilyC 0.01241707 0.036296939 0.3420968 7.324227e-01
## degreeD -0.01463775 0.003439979 -4.2551839 2.497117e-05
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0897679698 0.015102479 5.94392300 5.243614e-09
## homophilyC -0.0007352552 0.031663674 -0.02322078 9.814835e-01
## degreeD -0.0078633835 0.002581445 -3.04611709 2.441337e-03
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).


## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.143453337 0.013196947 10.8701914 7.915218e-25
## homophilyD -0.004574247 0.022897138 -0.1997737 8.417396e-01
## degreeD -0.016621520 0.003429577 -4.8465214 1.684099e-06
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.153599485 0.012214752 12.5749166 1.114121e-31
## homophilyD 0.004487794 0.020768802 0.2160834 8.290114e-01
## degreeD -0.019089153 0.003385206 -5.6389926 2.872815e-08
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.16953223 0.013875412 12.2181763 3.390468e-30
## homophilyD 0.01708714 0.017819198 0.9589173 3.380676e-01
## degreeD -0.02534844 0.003224604 -7.8609468 2.392511e-14
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.12444513 0.016172464 7.694877 7.681422e-14
## homophilyD -0.01955736 0.018494114 -1.057491 2.908013e-01
## degreeD -0.01319283 0.003169258 -4.162749 3.706307e-05
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.092690589 0.015035787 6.164665 1.466666e-09
## homophilyD -0.019100482 0.016291372 -1.172429 2.415887e-01
## degreeD -0.007427455 0.002390075 -3.107624 1.994397e-03
## Loading data last updated on 2023-01-20 21:50:22
## Call model1.visible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 21:54:00
## Call model2.visible(redo=TRUE) to update data.
## Loading data last updated on 2023-01-20 22:02:59
## Call model3(redo=TRUE) to update data.


## Warning: Removed 1 rows containing non-finite values (`stat_summary()`).
## Warning: Removed 1 rows containing non-finite values (`stat_compare_means()`).






## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.135174254 0.013088390 10.32779859 8.849968e-23
## homophilyC -0.001802408 0.026529037 -0.06794094 9.458600e-01
## degreeD -0.020121009 0.002074977 -9.69697743 1.781259e-20
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.16199443 0.012781109 12.674521 4.274370e-32
## homophilyC -0.10243596 0.040819055 -2.509513 1.240669e-02
## degreeD -0.01982692 0.003489878 -5.681265 2.279385e-08
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.1613482 0.011647035 13.853158 3.890768e-37
## homophilyC -0.1253464 0.033191899 -3.776416 1.783518e-04
## degreeD -0.0203117 0.002951662 -6.881446 1.790917e-11
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.113938792 0.011282449 10.09876415 6.173527e-22
## homophilyC 0.000325954 0.024971035 0.01305328 9.895905e-01
## degreeD -0.017783399 0.002366586 -7.51436706 2.681419e-13
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.077628607 0.009974949 7.782356 4.157447e-14
## homophilyC -0.040046644 0.020913358 -1.914883 5.608109e-02
## degreeD -0.007900679 0.001705004 -4.633819 4.592077e-06
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2 rows containing non-finite values (`stat_cor()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).


## [1] "Regression on proportion of ever-isolated individuals, Control ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.136038838 0.010525802 12.9243208 3.955062e-33
## homophilyD 0.008729852 0.018262614 0.4780177 6.328487e-01
## degreeD -0.020949587 0.002735409 -7.6586661 9.950491e-14
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.25 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.146161089 0.011583520 12.6180196 7.363568e-32
## homophilyD -0.005966099 0.019695516 -0.3029166 7.620802e-01
## degreeD -0.023527999 0.003210266 -7.3289866 9.455079e-13
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.5 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.14577006 0.011434408 12.748370 2.134952e-32
## homophilyD 0.01761919 0.014684392 1.199859 2.307671e-01
## degreeD -0.02658109 0.002657322 -10.002962 1.396214e-21
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 0.75 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.114079561 0.011136942 10.2433470 1.806829e-22
## homophilyD -0.002261114 0.012735714 -0.1775412 8.591556e-01
## degreeD -0.017664694 0.002182465 -8.0939181 4.476998e-15
## [1] "Regression on proportion of ever-isolated individuals, degree %ile = 1 ; "
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.074232567 0.009990304 7.4304612 4.784124e-13
## homophilyD -0.002771212 0.010824559 -0.2560116 7.980483e-01
## degreeD -0.009141652 0.001588050 -5.7565265 1.507711e-08
plot.trends <-
data.frame(
trends.df %>%
group_by(round, V, GINI, fractionCoop) %>%
summarize_all(list(mean=~mean(., na.rm=TRUE),sd=~sd(., na.rm=TRUE)))
)
plot.trends$V = factor(plot.trends$V)
plot.trends$GINI = factor(plot.trends$GINI)
for(i in unique(plot.trends$fractionCoop)){
g.gini = ggplot(data=plot.trends[plot.trends$fractionCoop==i,], aes(x=round,y=gini_mean,group=interaction(GINI,V))) +
geom_line(aes(color=GINI,linetype=V)) +
geom_ribbon(aes(ymin = gini_mean - gini_sd, ymax = gini_mean + gini_sd, fill=GINI),alpha=0.3) +
xlab("Round")+
ylab("gini") +
theme_bw()
g.gmd = ggplot(data=plot.trends[plot.trends$fractionCoop==i,], aes(x=round,y=gmd_mean,group=interaction(GINI,V))) +
geom_line(aes(color=GINI,linetype=V)) +
geom_ribbon(aes(ymin = gmd_mean - gmd_sd, ymax = gmd_mean + gmd_sd, fill=GINI),alpha=0.3) +
xlab("Round")+
ylab("gmd") +
theme_bw()
g.avg_wealth = ggplot(data=plot.trends[plot.trends$fractionCoop==i,], aes(x=round,y=avg_wealth_mean,group=interaction(GINI,V))) +
geom_line(aes(color=GINI,linetype=V)) +
geom_ribbon(aes(ymin = avg_wealth_mean - avg_wealth_sd, ymax = avg_wealth_mean + avg_wealth_sd, fill=GINI),alpha=0.3) +
xlab("Round")+
ylab("avg_wealth") +
theme_bw()
g.avg_coop = ggplot(data=plot.trends[plot.trends$fractionCoop==i,], aes(x=round,y=avg_coop_mean,group=interaction(GINI,V))) +
geom_line(aes(color=GINI,linetype=V)) +
geom_ribbon(aes(ymin = avg_coop_mean - avg_coop_sd, ymax = avg_coop_mean + avg_coop_sd, fill=GINI),alpha=0.3) +
xlab("Round")+
ylab("avg_coop") +
theme_bw()
g.avg_degree = ggplot(data=plot.trends[plot.trends$fractionCoop==i,], aes(x=round,y=avg_degree_mean,group=interaction(GINI,V))) +
geom_line(aes(color=GINI,linetype=V)) +
geom_ribbon(aes(ymin = avg_degree_mean - avg_degree_sd, ymax = avg_degree_mean + avg_degree_sd, fill=GINI),alpha=0.3) +
xlab("Round")+
ylab("avg_degree") +
theme_bw()
plot <- ggarrange(g.gini,g.gmd,g.avg_wealth,g.avg_coop,g.avg_degree,common.legend = TRUE,legend="bottom")
print(annotate_figure(plot, top = text_grob(paste("Degree percentile of nodes assigned to defectors =",i), color = "black", face = "bold", size = 10)))
}
## Warning: Removed 6 rows containing missing values (`geom_line()`).
## Warning: Removed 6 rows containing missing values (`geom_line()`).

## Warning: Removed 6 rows containing missing values (`geom_line()`).

## Warning: Removed 6 rows containing missing values (`geom_line()`).

## Warning: Removed 6 rows containing missing values (`geom_line()`).


fig1 = ggplot(data = df.netIntLowDegree,
aes(x = degreeD, y = homophilyC, color = percentIsolation*100)) +
geom_point() +
scale_x_continuous("Mean degree of defectors") +
scale_y_continuous("C-assortativity") +
scale_color_viridis(option = "magma") +
labs(color="Isolated \nindividuals (%)")
fig2 = ggplot(data = df.netIntLowDegree,
aes(x = degreeD, y = homophilyD, color = percentIsolation*100)) +
geom_point() +
scale_x_continuous("Mean degree of defectors") +
scale_y_continuous("D-assortativity") +
scale_color_viridis(option = "magma") +
labs(color="Isolated \nindividuals (%)")
fig3 = ggplot(data = df.netIntLowDegree,
aes(x = degreeD, y = heterophily, color = percentIsolation*100)) +
geom_point() +
scale_x_continuous("Mean degree of defectors") +
scale_y_continuous("Heterophily") +
scale_color_viridis(option = "magma") +
labs(color="Isolated \nindividuals (%)")
fig4 = ggplot(data = df.netIntLowDegree,
aes(x = degreeC, y = homophilyC, color = percentIsolation*100)) +
geom_point() +
scale_x_continuous("Mean degree of cooperators") +
scale_y_continuous("C-assortativity") +
scale_color_viridis(option = "magma") +
labs(color="Isolated \nindividuals (%)")
fig5 = ggplot(data = df.netIntLowDegree,
aes(x = degreeC, y = homophilyD, color = percentIsolation*100)) +
geom_point() +
scale_x_continuous("Mean degree of cooperators") +
scale_y_continuous("D-assortativity") +
scale_color_viridis(option = "magma") +
labs(color="Isolated \nindividuals (%)")
fig6 = ggplot(data = df.netIntLowDegree,
aes(x = degreeC, y = heterophily, color = percentIsolation*100)) +
geom_point() +
scale_x_continuous("Mean degree of cooperators") +
scale_y_continuous("Heterophily") +
scale_color_viridis(option = "magma") +
labs(color="Isolated \nindividuals (%)")
fig7 = ggplot(data = df.netIntLowDegree,
aes(x = degreeC, y = degreeD, color = percentIsolation*100)) +
geom_point() +
scale_x_continuous("Mean degree of cooperators") +
scale_y_continuous("Mean degree of defectors") +
scale_color_viridis(option = "magma") +
labs(color="Isolated \nindividuals (%)")
print(ggarrange(fig1,fig2,fig3,fig4,fig5,fig6,fig7,common.legend = TRUE,legend="right"))
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 5 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 5 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).

fig1 = ggplot(data = df.netIntLowDegree,
aes(x = degreeC, y = percentIsolation*100)) +
geom_point() +
scale_x_continuous("Mean degree of cooperators") +
scale_y_continuous("Isolated individuals (%)")
fig2 = ggplot(data = df.netIntLowDegree,
aes(x = degreeD, y = percentIsolation*100)) +
geom_point() +
scale_x_continuous("Mean degree of defectors") +
scale_y_continuous("Isolated individuals (%)")
fig3 = ggplot(data = df.netIntLowDegree,
aes(x = homophilyC, y = percentIsolation*100)) +
geom_point() +
scale_x_continuous("C-assortativity") +
scale_y_continuous("Isolated individuals (%)")
fig4 = ggplot(data = df.netIntLowDegree,
aes(x = homophilyD, y = percentIsolation*100)) +
geom_point() +
scale_x_continuous("D-assortativity") +
scale_y_continuous("Isolated individuals (%)")
print(ggarrange(fig1,fig2,fig3,fig4,common.legend = TRUE,legend="right"))
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 5 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 5 rows containing missing values (`geom_point()`).

reg.isolation = glm(percentIsolation*100 ~ degreeC + degreeD + homophilyC + homophilyD + heterophily, data=df.netIntLowDegree, family = gaussian(link = "identity"))
summary(reg.isolation)
##
## Call:
## glm(formula = percentIsolation * 100 ~ degreeC + degreeD + homophilyC +
## homophilyD + heterophily, family = gaussian(link = "identity"),
## data = df.netIntLowDegree)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -8.9834 -3.0910 -0.7809 2.3558 21.0731
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.4663 0.6458 20.851 < 2e-16 ***
## degreeC 0.5172 0.1933 2.675 0.00752 **
## degreeD -1.3830 0.1277 -10.834 < 2e-16 ***
## homophilyC -9.1526 1.9184 -4.771 1.94e-06 ***
## homophilyD -1.3634 0.7689 -1.773 0.07634 .
## heterophily -8.7733 2.7288 -3.215 0.00132 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 18.4129)
##
## Null deviance: 58722 on 2494 degrees of freedom
## Residual deviance: 45830 on 2489 degrees of freedom
## (5 observations deleted due to missingness)
## AIC: 14357
##
## Number of Fisher Scoring iterations: 2
#variance inflation factor
car::vif(reg.isolation)
## degreeC degreeD homophilyC homophilyD heterophily
## 4.127914 3.441364 2.446945 1.404649 3.330138
reg.isolation = glm(percentIsolation*100 ~ degreeD + homophilyD, data=df.netIntLowDegree, family = gaussian(link = "identity"))
summary(reg.isolation)
##
## Call:
## glm(formula = percentIsolation * 100 ~ degreeD + homophilyD,
## family = gaussian(link = "identity"), data = df.netIntLowDegree)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -8.9748 -3.1617 -0.8096 2.2692 20.7265
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.11131 0.36889 32.831 <2e-16 ***
## degreeD -1.78448 0.07021 -25.416 <2e-16 ***
## homophilyD -0.08180 0.66198 -0.124 0.902
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 18.58674)
##
## Null deviance: 58722 on 2494 degrees of freedom
## Residual deviance: 46318 on 2492 degrees of freedom
## (5 observations deleted due to missingness)
## AIC: 14377
##
## Number of Fisher Scoring iterations: 2
#variance inflation factor
car::vif(reg.isolation)
## degreeD homophilyD
## 1.031338 1.031338
#double machine learning
library(DoubleML)
library(mlr3)
library(mlr3learners)
set.seed(3141)
##degreeC
dml_data = DoubleMLData$new(df.netIntLowDegree[complete.cases(df.netIntLowDegree[c("percentIsolation","degreeC","degreeD","homophilyC","homophilyD","heterophily")]),],
y_col = "percentIsolation",
d_cols = "degreeC",
x_cols = c("degreeD","homophilyC","homophilyD","heterophily"))
print(dml_data)
## ================= DoubleMLData Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): degreeC
## Covariates: degreeD, homophilyC, homophilyD, heterophily
## Instrument(s):
## No. Observations: 2495
# surpress messages from mlr3 package during fitting
lgr::get_logger("mlr3")$set_threshold("warn")
learner = lrn("regr.ranger", num.trees=500, mtry=floor(sqrt(4)), max.depth=5, min.node.size=2)
ml_l = learner$clone()
ml_m = learner$clone()
obj_dml_plr = DoubleMLPLR$new(dml_data, ml_l=ml_l, ml_m=ml_m)
obj_dml_plr$fit()
print(obj_dml_plr)
## ================= DoubleMLPLR Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): degreeC
## Covariates: degreeD, homophilyC, homophilyD, heterophily
## Instrument(s):
## No. Observations: 2495
##
## ------------------ Score & algorithm ------------------
## Score function: partialling out
## DML algorithm: dml2
##
## ------------------ Machine learner ------------------
## ml_l: regr.ranger
## ml_m: regr.ranger
##
## ------------------ Resampling ------------------
## No. folds: 5
## No. repeated sample splits: 1
## Apply cross-fitting: TRUE
##
## ------------------ Fit summary ------------------
## Estimates and significance testing of the effect of target variables
## Estimate. Std. Error t value Pr(>|t|)
## degreeC 0.003822 0.001767 2.163 0.0305 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##degreeD
dml_data = DoubleMLData$new(df.netIntLowDegree[complete.cases(df.netIntLowDegree[c("percentIsolation","degreeC","degreeD","homophilyC","homophilyD","heterophily")]),],
y_col = "percentIsolation",
d_cols = "degreeD",
x_cols = c("degreeC","homophilyC","homophilyD","heterophily"))
print(dml_data)
## ================= DoubleMLData Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): degreeD
## Covariates: degreeC, homophilyC, homophilyD, heterophily
## Instrument(s):
## No. Observations: 2495
# surpress messages from mlr3 package during fitting
lgr::get_logger("mlr3")$set_threshold("warn")
learner = lrn("regr.ranger", num.trees=500, mtry=floor(sqrt(4)), max.depth=5, min.node.size=2)
ml_l = learner$clone()
ml_m = learner$clone()
obj_dml_plr = DoubleMLPLR$new(dml_data, ml_l=ml_l, ml_m=ml_m)
obj_dml_plr$fit()
print(obj_dml_plr)
## ================= DoubleMLPLR Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): degreeD
## Covariates: degreeC, homophilyC, homophilyD, heterophily
## Instrument(s):
## No. Observations: 2495
##
## ------------------ Score & algorithm ------------------
## Score function: partialling out
## DML algorithm: dml2
##
## ------------------ Machine learner ------------------
## ml_l: regr.ranger
## ml_m: regr.ranger
##
## ------------------ Resampling ------------------
## No. folds: 5
## No. repeated sample splits: 1
## Apply cross-fitting: TRUE
##
## ------------------ Fit summary ------------------
## Estimates and significance testing of the effect of target variables
## Estimate. Std. Error t value Pr(>|t|)
## degreeD -0.01463 0.00105 -13.93 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##homophilyC
dml_data = DoubleMLData$new(df.netIntLowDegree[complete.cases(df.netIntLowDegree[c("percentIsolation","degreeC","degreeD","homophilyC","homophilyD","heterophily")]),],
y_col = "percentIsolation",
d_cols = "homophilyC",
x_cols = c("degreeC","degreeD","homophilyD","heterophily"))
print(dml_data)
## ================= DoubleMLData Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): homophilyC
## Covariates: degreeC, degreeD, homophilyD, heterophily
## Instrument(s):
## No. Observations: 2495
# surpress messages from mlr3 package during fitting
lgr::get_logger("mlr3")$set_threshold("warn")
learner = lrn("regr.ranger", num.trees=500, mtry=floor(sqrt(4)), max.depth=5, min.node.size=2)
ml_l = learner$clone()
ml_m = learner$clone()
obj_dml_plr = DoubleMLPLR$new(dml_data, ml_l=ml_l, ml_m=ml_m)
obj_dml_plr$fit()
print(obj_dml_plr)
## ================= DoubleMLPLR Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): homophilyC
## Covariates: degreeC, degreeD, homophilyD, heterophily
## Instrument(s):
## No. Observations: 2495
##
## ------------------ Score & algorithm ------------------
## Score function: partialling out
## DML algorithm: dml2
##
## ------------------ Machine learner ------------------
## ml_l: regr.ranger
## ml_m: regr.ranger
##
## ------------------ Resampling ------------------
## No. folds: 5
## No. repeated sample splits: 1
## Apply cross-fitting: TRUE
##
## ------------------ Fit summary ------------------
## Estimates and significance testing of the effect of target variables
## Estimate. Std. Error t value Pr(>|t|)
## homophilyC -0.07248 0.01630 -4.447 8.72e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##homophilyD
dml_data = DoubleMLData$new(df.netIntLowDegree[complete.cases(df.netIntLowDegree[c("percentIsolation","degreeC","degreeD","homophilyC","homophilyD","heterophily")]),],
y_col = "percentIsolation",
d_cols = "homophilyD",
x_cols = c("degreeC","degreeD","homophilyC","heterophily"))
print(dml_data)
## ================= DoubleMLData Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): homophilyD
## Covariates: degreeC, degreeD, homophilyC, heterophily
## Instrument(s):
## No. Observations: 2495
# surpress messages from mlr3 package during fitting
lgr::get_logger("mlr3")$set_threshold("warn")
learner = lrn("regr.ranger", num.trees=500, mtry=floor(sqrt(4)), max.depth=5, min.node.size=2)
ml_l = learner$clone()
ml_m = learner$clone()
obj_dml_plr = DoubleMLPLR$new(dml_data, ml_l=ml_l, ml_m=ml_m)
obj_dml_plr$fit()
print(obj_dml_plr)
## ================= DoubleMLPLR Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): homophilyD
## Covariates: degreeC, degreeD, homophilyC, heterophily
## Instrument(s):
## No. Observations: 2495
##
## ------------------ Score & algorithm ------------------
## Score function: partialling out
## DML algorithm: dml2
##
## ------------------ Machine learner ------------------
## ml_l: regr.ranger
## ml_m: regr.ranger
##
## ------------------ Resampling ------------------
## No. folds: 5
## No. repeated sample splits: 1
## Apply cross-fitting: TRUE
##
## ------------------ Fit summary ------------------
## Estimates and significance testing of the effect of target variables
## Estimate. Std. Error t value Pr(>|t|)
## homophilyD -0.009701 0.006258 -1.55 0.121
##heterophily
dml_data = DoubleMLData$new(df.netIntLowDegree[complete.cases(df.netIntLowDegree[c("percentIsolation","degreeC","degreeD","homophilyC","homophilyD","heterophily")]),],
y_col = "percentIsolation",
d_cols = "heterophily",
x_cols = c("degreeC","degreeD","homophilyC","homophilyD"))
print(dml_data)
## ================= DoubleMLData Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): heterophily
## Covariates: degreeC, degreeD, homophilyC, homophilyD
## Instrument(s):
## No. Observations: 2495
# surpress messages from mlr3 package during fitting
lgr::get_logger("mlr3")$set_threshold("warn")
learner = lrn("regr.ranger", num.trees=500, mtry=floor(sqrt(4)), max.depth=5, min.node.size=2)
ml_l = learner$clone()
ml_m = learner$clone()
obj_dml_plr = DoubleMLPLR$new(dml_data, ml_l=ml_l, ml_m=ml_m)
obj_dml_plr$fit()
print(obj_dml_plr)
## ================= DoubleMLPLR Object ==================
##
##
## ------------------ Data summary ------------------
## Outcome variable: percentIsolation
## Treatment variable(s): heterophily
## Covariates: degreeC, degreeD, homophilyC, homophilyD
## Instrument(s):
## No. Observations: 2495
##
## ------------------ Score & algorithm ------------------
## Score function: partialling out
## DML algorithm: dml2
##
## ------------------ Machine learner ------------------
## ml_l: regr.ranger
## ml_m: regr.ranger
##
## ------------------ Resampling ------------------
## No. folds: 5
## No. repeated sample splits: 1
## Apply cross-fitting: TRUE
##
## ------------------ Fit summary ------------------
## Estimates and significance testing of the effect of target variables
## Estimate. Std. Error t value Pr(>|t|)
## heterophily -0.08188 0.02168 -3.776 0.000159 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
fig1 = ggplot(data = df.netIntLowDegree,
aes(x = degreeD, y = homophilyC, color = avgCoopFinal*100)) +
geom_point() +
scale_x_continuous("Mean degree of defectors") +
scale_y_continuous("C-assortativity") +
scale_color_viridis(option = "magma") +
labs(color="Cooperation in \nfinal round (%)")
fig2 = ggplot(data = df.netIntLowDegree,
aes(x = degreeD, y = homophilyD, color = avgCoopFinal*100)) +
geom_point() +
scale_x_continuous("Mean degree of defectors") +
scale_y_continuous("D-assortativity") +
scale_color_viridis(option = "magma") +
labs(color="Cooperation in \nfinal round (%)")
fig3 = ggplot(data = df.netIntLowDegree,
aes(x = degreeD, y = heterophily, color = avgCoopFinal*100)) +
geom_point() +
scale_x_continuous("Mean degree of defectors") +
scale_y_continuous("Heterophily") +
scale_color_viridis(option = "magma") +
labs(color="Cooperation in \nfinal round (%)")
fig4 = ggplot(data = df.netIntLowDegree,
aes(x = degreeC, y = homophilyC, color = avgCoopFinal*100)) +
geom_point() +
scale_x_continuous("Mean degree of cooperators") +
scale_y_continuous("C-assortativity") +
scale_color_viridis(option = "magma") +
labs(color="Cooperation in \nfinal round (%)")
fig5 = ggplot(data = df.netIntLowDegree,
aes(x = degreeC, y = homophilyD, color = avgCoopFinal*100)) +
geom_point() +
scale_x_continuous("Mean degree of cooperators") +
scale_y_continuous("D-assortativity") +
scale_color_viridis(option = "magma") +
labs(color="Cooperation in \nfinal round (%)")
fig6 = ggplot(data = df.netIntLowDegree,
aes(x = degreeC, y = heterophily, color = avgCoopFinal*100)) +
geom_point() +
scale_x_continuous("Mean degree of cooperators") +
scale_y_continuous("Heterophily") +
scale_color_viridis(option = "magma") +
labs(color="Cooperation in \nfinal round (%)")
fig7 = ggplot(data = df.netIntLowDegree,
aes(x = degreeC, y = degreeD, color = avgCoopFinal*100)) +
geom_point() +
scale_x_continuous("Mean degree of cooperators") +
scale_y_continuous("Mean degree of defectors") +
scale_color_viridis(option = "magma") +
labs(color="Cooperation in \nfinal round (%)")
print(ggarrange(fig1,fig2,fig3,fig4,fig5,fig6,fig7,common.legend = TRUE,legend="right"))
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 5 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Warning: Removed 5 rows containing missing values (`geom_point()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).
## Removed 1 rows containing missing values (`geom_point()`).
